<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="lib/vue.global.js"></script>
</head>
<body>
  <div id="root">
    <my-input v-model:val = 'userName' clearable></my-input>
    <my-input type="password" v-model:val = 'password'></my-input>
    <button @click="login">登录</button>
  </div>
</body>
<script>
  const myInput = {
    emits: ['update:val'],
    // emits: {
    //   'update:val' () {
    //     return true
    //   }
    // },
    // props: ['val'],
    props: {
      val: String,
      type: {
        type: String,
        default: 'text'
      },
      clearable: {
        type: Boolean,
        default: false
      }
    },
    template: `
      <div>
        <input :type="type" :value="val" @input="sendData">
        <button v-if="clearable" @click="clear">X</button>
      </div>
    `,
    methods: {
      sendData (event) {
        this.$emit('update:val', event.target.value)
      },
      clear () {
        this.$emit('update:val', '')
      }
    }
  }

  const { createApp } = window.Vue
  const app = createApp({
    data () {
      return {
        userName: '',
        password: ''
      }
    },
    methods: {
      getUserName (val) {
        this.userName = val
      },
      getPassword (val) {
        this.password = val
      },
      login () {
        console.log({
          userName: this.userName,
          password: this.password
        })
      }
    },
    components: {
      myInput
    }
  })
  app.mount('#root')
</script>
</html>